package com.ruifu.common.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import java.util.Arrays;
import java.util.List;
import java.util.Properties;

/**
 * 委托链操作工具类
 *
 * @author nyr
 * @create 2019/9/9
 */
public class AssignorsUtils {

    /**
     * 获取指定用户id在委托链的起始索引
     *
     * @param assignors
     * @param userId
     * @return
     */
    public static int getIndexOf(String assignors, String userId) {
        //将字符串前后拼接,
        String str = "," + assignors + ",";
        return str.indexOf("," + userId + ",");
    }

    /**
     * 获取指定用户id在委托链的最后索引
     *
     * @param assignors
     * @param userId
     * @return
     */
    public static int getLastIndexOf(String assignors, String userId) {
        //将字符串前后拼接,
        String str = "," + assignors + ",";
        return str.lastIndexOf("," + userId + ",");
    }

    /**
     * 获取指定用户id是否在委托链中
     *
     * @param assignors
     * @param userId
     * @return
     */
    public static boolean isContains(String assignors, String userId) {
        //将字符串前后拼接,
        String str = "," + assignors + ",";
        return str.contains("," + userId + ",");
    }

    /**
     * 判断指定用户是否为当前审核的人
     *
     * @param assignors 委托链
     * @param audits    审核链
     * @param userId    用户id
     * @return
     */
    public static boolean isAudit(String assignors, String audits, String userId) {
        String superior = null;
        //判断审核链是否为空
        if (audits == null || "".equals(audits)) {
            //从委托链获取最后用户作为上级用户
            superior = assignors.substring(assignors.lastIndexOf(",") + 1);
        } else {
            //获取审核链最后的用户作为下级
            String junior = audits.substring(audits.lastIndexOf(",") + 1);
            /*通过委托链和下级寻找上级用户*/
            //获取下级在委托链中的索引
            int indexOf = AssignorsUtils.getIndexOf(assignors, junior);
            //判断索引是否已经到顶
            if (indexOf == 0) {
                return false;
            }
            //切割委托链舍弃下级之后
            String newAssignors = assignors.substring(0, indexOf - 1);
            //从新委托链中获取上级
            superior = newAssignors.substring(newAssignors.lastIndexOf(",") + 1);
        }
        /*校验当前用户是否能够审核*/
        return userId.equals(superior);
    }

    /**
     * 修改第一委托人
     *
     * @param assignors 委托链
     * @param userId    要修改的用户
     * @return
     */
    public static String updateFirstAssignor(String assignors, Long userId) {
        //根据委托链切割数组
        String[] assignorArrays = assignors.split(",");
        //修改第一委托人
        assignorArrays[0] = userId + "";
        //数组转换字符串  -> [1 ,3 ,3 ]
        String newAssignorArrays = Arrays.toString(assignorArrays);
        //得到更改后的委托链并返回
        return newAssignorArrays.replaceAll(" ", "").replaceAll("\\[", "").replaceAll("]", "");
    }

    /**
     * 根据传入的json数组集按照传入的转换规则转换
     *
     * @param dependency 依赖集
     * @param rule       转换规则
     * @return
     */
    public static String convertDependency(String dependency, String rule) {
        //待转换 ['192','193']
        List<String> dependencyList = JSON.parseObject(dependency, List.class);
        //规则 {'192':'1','193':'2'}
        Properties properties = JSON.parseObject(rule, Properties.class);
        for (int i = 0; i < dependencyList.size(); i++) {
            dependencyList.set(i, properties.getProperty(dependencyList.get(i)));
        }
        return JSONObject.toJSONString(dependencyList);
    }
}
